class SingleTextPosition(object):
    def __init__(self, single_line):
        """
        {"text": "营业执照", "score": 0.635, "bbox": [725, 445, 1758, 445, 1758, 731, 725, 731]}
        """
        bbox = single_line["bbox"]
        if len(bbox) == 8:
            new_bbox = [
                int((bbox[0] + bbox[6]) / 2),
                int((bbox[1] + bbox[3]) / 2),
                int((bbox[2] + bbox[4] - bbox[0] - bbox[6]) / 2),
                int((bbox[5] + bbox[7] - bbox[1] - bbox[3]) / 2)
            ]
        elif len(bbox) == 4:
            new_bbox = bbox
        else:
            raise Exception("bbox type is error")
        # 返回 {"text": "营业执照", "score": 0.635, "bbox": [1758, 731, 725, 731]}
        self.text = single_line["text"]
        self.newBbox = new_bbox
        self.srcBbox = bbox
        self.score = single_line["score"]

    @property
    def top_point(self):
        return self.newBbox[1]

    @property
    def left_point(self):
        return self.newBbox[0]

    @property
    def right_point(self):
        return self.left_point + self.newBbox[2]

    @property
    def bottom_point(self):
        return self.top_point + self.newBbox[3]

    @property
    def width(self):
        return self.newBbox[2]

    @property
    def height(self):
        return self.newBbox[3]


class ComparisonPosition(object):
    """
    以第一个text position为基准
    """
    def __init__(self, first, second):
        """
         first: {"text": "营业执照", "score": 0.635, "bbox": [725, 445, 1758, 445, 1758, 731, 725, 731]},
         second: {"text": "统一社会信用代码", "score": 0.75, "bbox": [225, 533, 581, 533, 581, 571, 225, 571]}
        """
        self.first = first
        self.second = second

    @property
    def height_gap(self):
        """两个文本之间高度差"""
        if self.second.top_point >= self.first.bottom_point:
            return self.second.top_point - self.first.bottom_point

        if self.first.top_point >= self.second.bottom_point:
            return self.first.top_point - self.second.bottom_point

        return 0

    @property
    def height_gap_ratio(self):
        """
        高度差占最矮文本的比例
        """
        return round(self.height_gap / max(self.first.height, self.second.height), 3)

    @property
    def height_ratio(self):
        """两个文本高度比例"""
        return round(min(self.first.height, self.second.height) / max(self.first.height, self.second.height), 3)

    @property
    def width_gap(self):
        """两个文本宽度差"""
        if self.second.left_point >= self.first.right_point:
            return self.second.left_point - self.first.right_point
        if self.first.left_point >= self.second.right_point:
            return self.first.left_point - self.second.right_point
        return 0

    @property
    def width_gap_ratio(self):
        """两个文本宽度差比例"""
        return round(self.width_gap / max(self.first.width, self.second.width), 3)

    @property
    def width_ratio(self):
        """两个文本宽度比例"""
        return round(min(self.first.width, self.second.width) / max(self.first.width, self.second.width), 3)

    @property
    def horizontal_over_lap(self):
        over_lap = min(self.first.right_point, self.second.right_point) - max(self.first.left_point,
                                                                              self.second.left_point)
        if over_lap < 0:
            return 0
        return over_lap

    @property
    def horizontal_overlap_ratio(self):
        return round(self.horizontal_over_lap / min(self.first.width, self.second.width), 3)

    @property
    def center_height_ratio(self):
        first_center_height = self.first.top_point + int(self.first.height / 2)
        second_center_height = self.second.top_point + int(self.second.height / 2)
        return round(abs(second_center_height - first_center_height) / max(self.first.height, self.second.height), 3)
